package com.myworkbench.workbench.domain.entity.resource;

import com.myworkbench.workbench.domain.Identity;
import com.myworkbench.workbench.domain.entity.role.RoleId;
import com.myworkbench.workbench.domain.entity.user.UserId;
import com.myworkbench.workbench.domain.valueobject.Creator;
import com.myworkbench.workbench.domain.valueobject.Updator;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.Set;

@Data
@Table
@Entity
@AllArgsConstructor
@NoArgsConstructor
@NamedNativeQuery(name = "Resource.findAllResourceByUserId", query = ResourceNamedSQL.findAllResourceByUserId,resultClass=Resource.class)
public class Resource extends Identity<Resource> {
    @Transient
    private ResourceId resourceId;

    @Column(length = 50,nullable = false)
    private String name;
    /**
     *  重写属性
     */
    @AttributeOverrides({
       @AttributeOverride(name="id",column=@Column(name="parent_id"))
    })
     private ResourceId parent;
    /**
     * 菜单图标
     */
    @Column(length = 20)
    private String resourceIcon;

    @Column
    @Enumerated(EnumType.ORDINAL)
    private ResourceType type ;

     // 默认启用
    @Column(nullable = false)
    @Enumerated(EnumType.ORDINAL)
    private ResourceStatus status=ResourceStatus.ENABLE;
    /**
     *  资源编码
     */
    @Column(length = 50,nullable = false)
    private String code;
    /**
     *  索引编号
     */
    private int indexNo = 0;
    /**
     *  资源描述
     */
    @Column(length = 200)
    private String resourceDesc;

    private Short isDelete = 0;

    //    角色资源关联
    @ElementCollection
    @CollectionTable(name="rel_role_resource",
            joinColumns = @JoinColumn(name="resource_id",foreignKey = @ForeignKey(name="none",value = ConstraintMode.NO_CONSTRAINT)))
    private Set<RoleId> roleIds;

    public ResourceId getResourceId() {
        return new ResourceId(this.getId());
    }

    /**
     * 资源启用
     */
    public void enable(){
        this.status = ResourceStatus.ENABLE;
    }

    /**
     *  资源禁用
     */
    public void disable(){
        this.status = ResourceStatus.DISABLE;
    }
    /**
     *  值对象
     */
    private Creator creator;
    private Updator updator;

}
